const Mock = require('mockjs')

const permissionData = Mock.mock({
  'permissions|20': [{
    'id|+1': 1,
    parentId: '@integer(0, 5)',
    name: '@ctitle(2, 4)',
    'type|1': [1, 2, 3], // 1:目录 2:菜单 3:按钮
    permissionKey: '@word(6)',
    path: '@url',
    component: '@word(5)',
    icon: '@word(5)',
    'status|1': [0, 1],
    orderNum: '@integer(1, 100)',
    createTime: '@datetime'
  }]
})

module.exports = [
  // 获取权限列表
  {
    url: '/api/system/permission/list',
    method: 'get',
    response: () => {
      return {
        code: 200,
        msg: '操作成功',
        data: permissionData.permissions
      }
    }
  },
  
  // 获取权限树结构
  {
    url: '/api/system/permission/tree',
    method: 'get',
    response: () => {
      // 构造树形结构数据
      const treeData = []
      const permissions = permissionData.permissions
      
      // 查找根节点
      permissions.forEach(permission => {
        if (permission.parentId === 0) {
          treeData.push({
            ...permission,
            children: []
          })
        }
      })
      
      // 添加子节点
      treeData.forEach(parent => {
        permissions.forEach(permission => {
          if (permission.parentId === parent.id) {
            parent.children.push(permission)
          }
        })
      })
      
      return {
        code: 200,
        msg: '操作成功',
        data: treeData
      }
    }
  },
  
  // 根据ID获取权限信息
  {
    url: RegExp('/api/system/permission/.*'),
    method: 'get',
    response: ({ url }) => {
      const permissionId = url.split('/').pop()
      const permission = permissionData.permissions.find(p => p.id == permissionId)
      if (permission) {
        return {
          code: 200,
          msg: '操作成功',
          data: permission
        }
      } else {
        return {
          code: 404,
          msg: '权限不存在'
        }
      }
    }
  },
  
  // 新增权限
  {
    url: '/api/system/permission/add',
    method: 'post',
    response: ({ body }) => {
      const newPermission = {
        id: permissionData.permissions.length + 1,
        parentId: body.parentId,
        name: body.name,
        type: body.type,
        permissionKey: body.permissionKey,
        path: body.path,
        component: body.component,
        icon: body.icon,
        status: body.status,
        orderNum: body.orderNum,
        createTime: new Date().toLocaleString('zh-CN')
      }
      permissionData.permissions.push(newPermission)
      return {
        code: 200,
        msg: '操作成功',
        data: null
      }
    }
  },
  
  // 更新权限
  {
    url: '/api/system/permission/update',
    method: 'put',
    response: ({ body }) => {
      const index = permissionData.permissions.findIndex(p => p.id == body.id)
      if (index !== -1) {
        permissionData.permissions[index] = { ...permissionData.permissions[index], ...body }
        return {
          code: 200,
          msg: '操作成功',
          data: null
        }
      } else {
        return {
          code: 404,
          msg: '权限不存在'
        }
      }
    }
  },
  
  // 删除权限
  {
    url: RegExp('/api/system/permission/delete/.*'),
    method: 'delete',
    response: ({ url }) => {
      const permissionId = url.split('/').pop()
      const index = permissionData.permissions.findIndex(p => p.id == permissionId)
      if (index !== -1) {
        permissionData.permissions.splice(index, 1)
        return {
          code: 200,
          msg: '操作成功',
          data: null
        }
      } else {
        return {
          code: 404,
          msg: '权限不存在'
        }
      }
    }
  },
  
  // 根据角色ID获取权限列表
  {
    url: RegExp('/api/system/permission/role/.*'),
    method: 'get',
    response: () => {
      return {
        code: 200,
        msg: '操作成功',
        data: []
      }
    }
  },
  
  // 根据用户ID获取权限列表
  {
    url: RegExp('/api/system/permission/user/.*'),
    method: 'get',
    response: () => {
      return {
        code: 200,
        msg: '操作成功',
        data: []
      }
    }
  }
]